# Based on the upstream weston aport
# Changes:
# - framebuffer no 0 hz patch for lg-mako, upstreamed here (this will be in the next weston release \o/):
#	https://patchwork.freedesktop.org/patch/150943/
# - APKBUILD arch changed to "all", upstreaming here:
#   https://github.com/alpinelinux/aports/pull/1689
pkgname=weston
pkgver=9999
_pkgver=2.0.0
pkgrel=2
_libname=lib$pkgname
_libdir=$_libname-${_pkgver%%.*}
pkgdesc="The reference Wayland server"
url="http://wayland.freedesktop.org"
arch="all"
license="MIT"
depends=""
makedepends="wayland-protocols libxkbcommon-dev xkeyboard-config libinput-dev libunwind-dev mtdev-dev libxcursor-dev glu-dev pango-dev colord-dev freerdp-dev libwebp-dev libva-dev dbus-dev"
_cms="cms-colord cms-static"
_shell="shell-desktop shell-fullscreen shell-ivi"
_client="info terminal wcap-decode"
_backend="backend-drm backend-fbdev backend-headless
	backend-rdp backend-x11 backend-wayland
	"
options="!check"

for _sub in $_cms $_shell $_client $_backend; do
	subpackages="$subpackages $pkgname-$_sub:_sub"
done
subpackages="$pkgname-dev $pkgname-doc $subpackages
	$pkgname-clients $_libname-desktop:_libd $_libname:libs
	$pkgname-xwayland $pkgname-desktop-x11:_x11:noarch
	"
source="
    http://wayland.freedesktop.org/releases/$pkgname-$_pkgver.tar.xz
    no_0hz_refresh_rate.patch
    0001-compositor-fbdev-Added-parameter-pixman-type.patch
    0002-compositor-fbdev-Add-support-for-ABGR.patch
    0003-compositor-fbdev-print-the-pixman-type-guessed-in-ca.patch
    "
builddir="$srcdir/$pkgname-$_pkgver"

build() {
	cd "$builddir"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--libexecdir=/usr/lib/$pkgname \
		--enable-rdp-compositor \
		--enable-screen-sharing \
		--enable-vaapi-recorder \
		--enable-clients \
		--enable-demo-clients-install \
		--disable-weston-launch \
		--disable-setuid-install \
		|| return 1
	make
}

check() {
	make -C "$builddir" check
	return 0
}

package() {
	make  -C "$builddir" DESTDIR="$pkgdir" install
}

libs() {
	depends="xkeyboard-config"
	default_libs
}

_libd() {
	pkgdesc="Desktop shells abstraction library for libweston compositors"
	mkdir -p "$subpkgdir"/usr/lib || return 1
	mv "$pkgdir"/usr/lib/*desktop* "$subpkgdir"/usr/lib
}

clients() {
	pkgdesc="Weston example clients"
	mkdir -p "$subpkgdir"/usr/bin || return 1
	mv "$pkgdir"/usr/bin/$pkgname-* "$subpkgdir"/usr/bin
}

xwayland() {
	pkgdesc="Wayland module to run X Clients"
	mkdir -p "$subpkgdir"/usr/lib/$_libdir || return 1
	mv "$pkgdir"/usr/lib/$_libdir/xwayland.so \
		"$subpkgdir"/usr/lib/$_libdir
}

_x11() {
	pkgdesc="Weston desktop helper pack (x11 backend)"
	depends="$pkgname $pkgname-shell-desktop $pkgname-xwayland
		$pkgname-backend-x11 $pkgname-terminal"
	mkdir -p "$subpkgdir"
}

_sub() {
	local name path
	case $subpkgname in
	*-cms-*)
		name=${subpkgname#$pkgname-cms-}
		path=/usr/lib/$pkgname
		pkgdesc="Weston CMS module: $name"
		name=cms-$name.so
		;;
	*-shell-*)
		name=${subpkgname#$pkgname-shell-}
		path=/usr/lib/$pkgname
		pkgdesc="Weston shell: $name"
		name=*$name-shell*
		;;
	*-backend-*)
		name=${subpkgname#$pkgname-backend-}
		path=/usr/lib/$_libdir
		pkgdesc="Weston backend: $name"
		name=$name-backend.so
		grep -q cairo "$pkgdir"/$path/$name && depends="mesa-dri-swrast"
		;;
	*)
		name=$subpkgname
		path=/usr/bin
		[ -x "$pkgdir"/$path/$name ] || name=${subpkgname#$pkgname-}
		pkgdesc="Weston client: $name"
		;;
	esac
	mkdir -p "$subpkgdir"/$path || return 1
	mv "$pkgdir"/$path/$name "$subpkgdir"/$path
}
sha512sums="085a0ba278932d41b50edd6e89db5df31cd6a1179c6cfe9a8ac5ac64e63b25cfc3da1ad8c587259273c3812593029b803867195e2d82b12b5cdd2588ac59acc6  weston-2.0.0.tar.xz
68d8485eed6a536924a8ebef7e0b45738330c38326fe659443c26d674f9538ec1c66033f83a7971914dcd72bc52333e3f55486c5a01e067e3f5fee8f5b489728  no_0hz_refresh_rate.patch
2daa68ee19f4e123d7f3148517c2afcd4df0f065815a0e28db38f301260cd833b7170060c46127e65a25021e2d814afb40fc0f2987cbb3ab5cd4f9dae778bc98  0001-compositor-fbdev-Added-parameter-pixman-type.patch
b4da8caa9373c2c23b9955c50b9d682de3ed17bb48974490d111fd366c7b2d0231a0a5b0e55fb6cb1b8d5fd9b435823f3d91ff625984ecfe3b779892f6ae8b08  0002-compositor-fbdev-Add-support-for-ABGR.patch
b5eb741ea8b6fcbd9de95e773fe0bf4ae6588ef57564f97a65aefc6c7ec29f1a01de9764a25672fd7c76c8ff514b497743cbaf279818123041c161c7a1e62bb6  0003-compositor-fbdev-print-the-pixman-type-guessed-in-ca.patch"
